package TeamR3.dao.impl;

import java.util.List;

import org.aspectj.apache.bcel.generic.ReturnaddressType;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import TeamR3.dao.UserDao;
import TeamR3.entity.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao{
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
    
	@Override
	public List<User> queryAllUsers() {
		return sqlSessionTemplate.selectList("queryAllUsers");
	}
	@Override
	public User getUser(String user_username, String user_password) {
		User tempUser = new User();
		tempUser.setUser_name(user_username);
		tempUser.setUser_password(user_password);
		System.out.println(tempUser);
        User resultUser = sqlSessionTemplate.selectOne("getUser", tempUser);
        System.out.println(resultUser);
        if(resultUser==null) return null;
        resultUser.setUser_password("");
        return resultUser;
	}
	@Override
	public boolean checkUnique(String user_name) {
		int count = sqlSessionTemplate.selectOne("checkUnique", user_name);
		if(count==1) return true;
		else return false;
	}
	@Override
	public int addUser(String user_name, int user_privilege,
			String user_password) {
		User tempUser = new User(user_name, user_privilege, user_password);
		return sqlSessionTemplate.insert("addUser", tempUser);
	}

	@Override
	public int modifyUser(String user_name, int user_privilege,
			String user_password) {
		User tempUser = new User(user_name, user_privilege, user_password);
		return sqlSessionTemplate.update("modifyUser", tempUser);
	}

	@Override
	public int delUser(String user_name) {
		if(this.checkUnique(user_name)){
			return sqlSessionTemplate.delete("delUser", user_name);
		}else return 0;
	}
}
